package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.DistributionTriggerMode;
import cn.b.sky.mes.mm.model.condition.DistributionTriggerModeCondition;
import cn.b.sky.mes.mm.service.DistributionTriggerModeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/distributionTriggerMode/*")
public class DistributionTriggerModeController extends SecurityController {

  @Resource
  DistributionTriggerModeService distributionTriggerModeService;

  @RequestMapping("distributionTriggerModeList")
  public String distributionTriggerModeList(@ModelAttribute("condition") DistributionTriggerModeCondition condition,
      String navTabId, Model model) {
    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }
    condition.setSiteCode(this.getSiteCode());
    List<DistributionTriggerMode> distributionTriggerModes = distributionTriggerModeService
        .selectDistributionTriggerModeByCondition(condition);
    int count = distributionTriggerModeService.selectDistributionTriggerModeCount(condition);
    condition.setData(distributionTriggerModes);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute("navTabId", navTabId);
    return "mm/distributionTriggerMode/distributionTriggerMode_list";
  }

  @RequestMapping("addDistributionTriggerMode")
  public String addDistributionTriggerMode(String navTabId, Model model) {
    model.addAttribute("navTabId", navTabId);
    return "mm/distributionTriggerMode/distributionTriggerMode_add";
  }

  @RequestMapping("editDistributionTriggerMode")
  public String editDistributionTriggerMode(String navTabId, Model model, String id) {
    model.addAttribute("navTabId", navTabId);
    DistributionTriggerMode mode = distributionTriggerModeService.selectDistributionTriggerMode(getSiteCode(), id);
    model.addAttribute("distributionTriggerMode", mode);
    return "mm/distributionTriggerMode/distributionTriggerMode_add";
  }

  @RequestMapping("viewDistributionTriggerMode")
  public String viewDistributionTriggerMode(String navTabId, Model model, String id) {
    model.addAttribute("navTabId", navTabId);
    DistributionTriggerMode mode = distributionTriggerModeService.selectDistributionTriggerMode(getSiteCode(), id);
    model.addAttribute("distributionTriggerMode", mode);
    return "mm/distributionTriggerMode/distributionTriggerMode_view";
  }

  @RequestMapping("isExistsDistributionTriggerMode")
  @ResponseBody
  public Boolean isExistsDistributionTriggerMode(Model model, String distributionTriggerModeCode) {
    if (StringUtils.isBlank(distributionTriggerModeCode)) {
      return false;
    }
    DistributionTriggerMode mode = distributionTriggerModeService.selectDistributionTriggerMode(getSiteCode(),
        distributionTriggerModeCode);
    if (mode != null && !StringUtils.isBlank(mode.getDistributionTriggerModeCode())) {
      return false;
    }
    return true;
  }

  @RequestMapping("saveDistributionTriggerMode")
  @ResponseBody
  public DWZResult saveDistributionTriggerMode(DistributionTriggerMode mode, Model model, String id, String navTabId) {
    if (mode == null || StringUtils.isBlank(mode.getDistributionTriggerModeCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterNull.M"));
    }
    mode.setSiteCode(this.getSiteCode());
    this.setCreateInfo(mode);
    if (StringUtils.isBlank(id)) {
      distributionTriggerModeService.insertDistributionTriggerMode(mode);
    } else {
      distributionTriggerModeService.updateDistributionTriggerMode(mode);
    }
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("deleteDistributionTriggerMode")
  @ResponseBody
  public DWZResult deleteDistributionTriggerMode(String navTabId, Model model, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = distributionTriggerModeService.deleteDistributionTriggerMode(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }

}
